package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Del implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");

        int current = bookList.getUsed();
        if (current == 0) {
            System.out.println("书架为空！");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();

        //寻找书架上是否有此书
        int index = -1;
        for (int i = 0; i < current; i++) {
            Book book1 = bookList.getBooks(i);
            if (book1.getName().equals(name)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("您要删除的书不在书架上！");
            return;
        }

        //current - 1:防止越界
        for (int i = index; i < current - 1; i++) {
            Book book = bookList.getBooks(i + 1);
            bookList.setBooks(i, book);
        }
        System.out.println("删除成功");

        bookList.setUsed(current - 1);
    }
}
